<?php

/**
 * Erra_Templating_View - Handles view
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Templating_View {

    protected $fields = array();
    public $pageParameters = array();
    public $isStatic = false;
    public $tpl_file = '';

    public function __construct($tpl_file) {
        $this->tpl_file = $tpl_file;
    }

    /**
     * Erra_Templating_View::displayView() - Displays a view
     *
     * @param mixed $args
     * @return
     */
    public function displayView($viewParameters) {
        $view_tpl_file = $this->tpl_file;
        // Prepare our layout to receive correct $_GET parameters
        if ($viewParameters != 'IGNORE_PARAMETERS') {
            $_GET = $viewParameters;
        }
        // Prepare root directory and view file
        $view_file = Erra_Core_Lexer::getCleanPath(DOCUMENT_ROOT . '/application/Views/' . tpl_get_full_path($view_tpl_file));

        // Prepare compilated file infos
        $file_compilated = basename($view_file) . '.phtml.compile.php';
        $compile_path = Erra_Core_Lexer::getCleanPath(DOCUMENT_ROOT . '/' . config_get("path.public_directory") . '/__cache/' . $file_compilated);

        $static_file = basename($view_file) . '.cache';
  

        // [CACHE FIXME] = This should be active only for admins or special roles !
        if (config_get('production_mode') == 0) {
            $tpl_contents = file_get_contents($view_file);
            $contents = Erra_Core_Lexer::ufmParse($tpl_contents);
            file_put_contents($compile_path, $contents);
        } 
 
        ob_start();
        if (isset($this->pageParameters['realRoute']) && is_array($this->pageParameters['realRoute'])) {
            extract($this->pageParameters['realRoute']);
        }
        include($compile_path);
        $result = ob_get_contents();
        ob_end_clean();  

        return $result;
    }

}